<HTML><HEAD><TITLE>read_directory(+Directory, +Pattern, -SubdirList, -FileList)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">Operating System</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>read_directory(+Directory, +Pattern, -SubdirList, -FileList)</H1>
Unifies SubdirList with a list of subdirectories and FileList with a list
of matching files in the specified directory.


<DL>
<DT><EM>Directory</EM></DT>
<DD>Atom or String
</DD>
<DT><EM>Pattern</EM></DT>
<DD>Atom or String
</DD>
<DT><EM>SubdirList</EM></DT>
<DD>Free variable or (maybe partial) list of strings
</DD>
<DT><EM>FileList</EM></DT>
<DD>Free variable or (maybe partial) list of strings
</DD>
</DL>
<H2>Description</H2>
   read_directory/4 scans the directory specified by Directory.  It
   collects the names of the subdirectories in SubdirList and the names of
   the files matching Pattern in FileList.  SubdirList and FileList are
   lists of strings.  Pattern is a string containing a filename
   specification similar to the usual UNIX convention.  The metacharacters
   recognised are:  * matches an arbitrary sequence of characters, ?
   matches any single character, [] matches one of the characters inside
   the brackets unless the first one is a ^ in which case it matches any
   character but those inside the brackets.

<P>
   Filenames beginning with a dot are not suppressed by default, but the
   directories "." and ".." are omitted from SubdirList.

<P>

<H3>Modes and Determinism</H3><UL>
<LI>read_directory(+, +, -, -) is det
</UL>
<H3>Exceptions</H3>
<DL>
<DT><EM>(4) instantiation fault </EM>
<DD>Directory or Pattern is not instantiated.
<DT><EM>(5) type error </EM>
<DD>Directory or Pattern is neither atom nor string.
<DT><EM>(5) type error </EM>
<DD>SubdirList or FileList are instantiated, but not to lists.
<DT><EM>(170) system interface error </EM>
<DD>An error occurred trying to access Directory.
</DL>
<H2>Examples</H2>
<PRE>
   Success:
   [eclipse]: read_directory("/usr/john", "*", Dirlist, Filelist).
   Dirlist = ["subdir1", "subdir2"]
   Filelist = ["one.c", "two.c", "three.pl", "four.pl"]
   yes.
   [eclipse]: read_directory(., "[^t]*.pl", Dirlist, Filelist).
   Dirlist = ["subdir1", "subdir2"]
   Filelist = ["four.pl"]
   yes.
   Fail:
   [eclipse]: read_directory(".", "*.c", _, ["one.c"]).
   no.
   Error:
   read_directory(_, "*", Dirs, Files).          (error 4)
   read_directory([46], "*", Dirs, Files).       (error 5)
   read_directory(".", _, Dirs, Files).          (error 4)
   read_directory(".", 3, Dirs, Files).          (error 5)
   read_directory(".", "*.pl", Dirs, file).      (error 5)
   read_directory("/private", "*", _, Files).    (error 170)


    % EXAMPLE: find_tree(+Root, +Pattern, -ListOfRelativeFiles)
    % Recursively find all files matching Pattern below the directory
    % or list of directories Roots.

    find_tree(Root, Pattern, AllFiles) :- (atom(Root);string(Root)), !,
	    find_dir(Root, "", "", Pattern, AllFiles, []).
    find_tree(Roots, Pattern, AllFiles) :-
	    find_tree(Roots, Pattern, AllFiles, []).

    find_tree([], _Pattern, AllFiles, AllFiles).
    find_tree([Root|Roots], Pattern, AllFiles, AllFiles0) :-
	    find_dir(Root, "", "", Pattern, AllFiles, AllFiles1),
	    find_tree(Roots, Pattern, AllFiles1, AllFiles0).

    find_dir(_, _, [], _, AllFiles, AllFiles) :- !.
    find_dir(Root, RelPath, [Dir|Dirs], Pattern, AllFiles, AllFiles0) :- !,
	    find_dir(Root, RelPath, Dir, Pattern, AllFiles, AllFiles1),
	    find_dir(Root, RelPath, Dirs, Pattern, AllFiles1, AllFiles0).
    find_dir(Root, RelPath, Dir, Pattern, AllFiles, AllFiles0) :-
	    ( RelPath=="" -&gt; RelDir=Dir
	    ; concat_string([RelPath,/,Dir], RelDir) ),
	    ( RelDir=="" -&gt; AbsDir=Root
	    ; concat_string([Root,/,RelDir], AbsDir) ),
	    read_directory(AbsDir, Pattern, SubDirs, Files),
	    find_files(Root, RelDir, Files, AllFiles, AllFiles1),
	    find_dir(Root, RelDir, SubDirs, Pattern, AllFiles1, AllFiles0).

    find_files(_, _, [], AbsFiles, AbsFiles).
    find_files(Root, RelDir, [File|Files], [AbsFile|AbsFiles], AbsFiles0) :-
	    ( RelDir=="" -&gt; concat_string([Root,/,File], AbsFile)
	    ; concat_string([Root,/,RelDir,/,File], AbsFile) ),
	    find_files(Root, RelDir, Files, AbsFiles, AbsFiles0).
</PRE>
<H2>See Also</H2>
<A HREF="../../kernel/opsys/cd-1.html">cd / 1</A>, <A HREF="../../kernel/opsys/getcwd-1.html">getcwd / 1</A>, <A HREF="../../kernel/env/get_flag-2.html">get_flag / 2</A>, <A HREF="../../kernel/opsys/pathname-4.html">pathname / 4</A>, <A HREF="../../kernel/opsys/existing_file-4.html">existing_file / 4</A>, <A HREF="../../kernel/opsys/exists-1.html">exists / 1</A>, <A HREF="../../kernel/opsys/mkdir-1.html">mkdir / 1</A>
</BODY></HTML>
